
// 1. 获取系统时间
// 2. 判断文件是否存在
// 3. 获取文件路径
// 4. 创建目录

// #pragma once
#ifndef __UTIL__
#define __UTIL__

#include <iostream>
#include <string>
#include <ctime>
#include <sys/stat.h>

namespace violet_log
{
    namespace tools
    {
        class get_time
        {
        public:
            static time_t _get_time()
            {
                return time(nullptr);
            }
        };

        class file
        {
        public:
            static bool exist(const std::string& path)
            {
                struct stat s;
                return stat(path.c_str(), &s) == 0;
            }

            static std::string get_path(const std::string& path)
            {
                size_t pos = path.find_last_of("/\\");
                if(pos == std::string::npos)
                    return ".";
                else
                    return path.substr(0, pos + 1);
            }

            static bool creat_directory(const std::string& path)
            {
                // /x/y/z/file.txt

                size_t pos = 0;
                while(pos < path.size())
                {
                    pos = path.find_first_of("/\\", pos);
                    if(pos == std::string::npos)
                    {
                        mkdir(path.c_str(), 0777);
                        return true;
                    }
                    else
                    {
                        ++pos; //跳过/或"\"
                        std::string dir = path.substr(0, pos);
                        if(exist(dir) == true)
                            continue;
                        else
                            mkdir(dir.c_str(), 0777);
                    }
                }
            }
        };
    }
}

#endif